package com.cy.store.service.impl;

import com.cy.store.entity.Cart;
import com.cy.store.entity.Product;
import com.cy.store.mapper.CartMapper;
import com.cy.store.service.ICartService;
import com.cy.store.service.IProductService;
import com.cy.store.service.ex.AccessDeniedException;
import com.cy.store.service.ex.CartNotFoundException;
import com.cy.store.service.ex.InsertException;
import com.cy.store.service.ex.UpdateException;
import com.cy.store.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @author: AngJie
 * @create: 2022-02-10 14:04
 **/
@Service
public class CartServiceImpl implements ICartService {

    @Autowired
    CartMapper cartMapper;

    //购物车的业务层依赖于购物车和商品的持久层
    @Autowired
    IProductService productService;

    @Override
    public void addToCart(Integer uid, Integer pid, Integer amount, String username) {

        //查询当前要添加的商品是否在购物车已存在
        Cart result = cartMapper.findByUidAndPid(uid, pid);
        Date date = new Date();
        if (result == null) { //当前商品没有被添加到购物车中
            Product product = productService.findById(pid);
            //创建Cart对象
            Cart cart = new Cart();
            //补全数据
            cart.setUid(uid);
            cart.setPid(pid);
            cart.setNum(amount);
            cart.setPrice(product.getPrice());
            cart.setCreatedUser(username);
            cart.setCreatedTime(date);
            cart.setModifiedUser(username);
            cart.setModifiedTime(date);

            Integer rows = cartMapper.insert(cart);
            if (rows != 1) {
                throw new InsertException("插入购物车数据时产生异常");
            }
        } else { //表示当前的商品在购物车中已经存在，则更新数据的num
            //将原先数量与新增数量相加
            Integer num = result.getNum() + amount;
            Integer rows = cartMapper.updateNumByCid(result.getCid(), num, username, date);
            if (rows != 1) {
                throw new UpdateException("更新购物车数据时产生未知异常");
            }
        }
    }

    @Override
    public List<CartVo> getVoByUid(Integer uid) {
        return cartMapper.findVoByUid(uid);
    }

    @Override
    public List<CartVo> getVoByCid(List<Integer> cids, Integer uid) {
        List<CartVo> list = cartMapper.findByCidList(cids);
        Iterator<CartVo> iterator = list.iterator();
        if (iterator.hasNext()) {
            CartVo cartVo = iterator.next();
            if (!cartVo.getUid().equals(uid)) { //表示当前的数据不属于当前用户
                //从集合中移除这个用户
                iterator.remove();
                // list.remove(cartVo);
            }
        }
        return list;
    }

    @Override
    public Integer addNum(Integer cid, String username, Integer uid, String operation) {
        Cart result = cartMapper.findByCid(cid);
        if (result == null) {
            throw new CartNotFoundException("用户的购物车数据不存在");
        }
        if (result.getUid() != uid) {
            throw new AccessDeniedException("数据非法访问");
        }
        Integer num = null;
        if ("add".equals(operation)) {
            num = result.getNum() + 1;
        } else
            num = result.getNum() - 1;
        Integer rows = cartMapper.updateNumByCid(cid, num, username, new Date());
        if (rows != 1) {
            throw new UpdateException("更新购物车商品数目失败");
        }

        //返回新的购物车数据总量
        return num;
    }


}
